Skip to content

t.stac: fixed issue with download and import asset potentially overwhelming network.#1641

Open
cwhite911 wants to merge 8 commits into
OSGeo:grass8from
cwhite911:t.stac_cache
Open

t.stac: fixed issue with download and import asset potentially overwhelming network.#1641
cwhite911 wants to merge 8 commits into
OSGeo:grass8from
cwhite911:t.stac_cache

Conversation

@cwhite911

Copy link
Copy Markdown
Contributor

Fixes bug with parallel download and import potentially overwhelming STAC API with requests if nproc is set too high.

@cwhite911 cwhite911 requested a review from petrasovaa March 12, 2026 21:10
@cwhite911 cwhite911 self-assigned this Mar 12, 2026
@cwhite911 cwhite911 added the bug Something isn't working label Mar 27, 2026
@cwhite911 cwhite911 requested a review from veroandreo April 22, 2026 18:14
@petrasovaa

Copy link
Copy Markdown
Contributor

The test is currently failing, but I didn't check whether that is related to this PR or not:

2026-04-22T18:39:28.9031811Z Running ./temporal/t.stac/libstac/testsuite/test_staclib.py...
2026-04-22T18:39:30.8914076Z ========================================================================
2026-04-22T18:39:30.8914659Z 
2026-04-22T18:39:30.8915130Z Downloading assets:   0%|          | 0/4 [00:00<?, ?it/s]
2026-04-22T18:39:30.8916424Z Downloading assets: 100%|██████████| 4/4 [00:00<00:00, 4029.11it/s]
2026-04-22T18:39:30.8917040Z F
2026-04-22T18:39:30.8917655Z Downloading assets:   0%|          | 0/2 [00:00<?, ?it/s]
2026-04-22T18:39:30.8918754Z Downloading assets: 100%|██████████| 2/2 [00:00<00:00, 4637.15it/s]
2026-04-22T18:39:30.8919338Z .
2026-04-22T18:39:30.8919754Z Downloading assets: 0it [00:00, ?it/s]
2026-04-22T18:39:30.8920590Z Downloading assets: 0it [00:00, ?it/s]
2026-04-22T18:39:30.8921069Z .
2026-04-22T18:39:30.8921665Z Downloading assets:   0%|          | 0/2 [00:00<?, ?it/s]
2026-04-22T18:39:30.8922500Z Downloading assets:   0%|          | 0/2 [00:00<?, ?it/s]
2026-04-22T18:39:30.8923068Z .
2026-04-22T18:39:30.8923561Z Downloading assets:   0%|          | 0/2 [00:00<?, ?it/s]
2026-04-22T18:39:30.8924492Z Downloading assets: 100%|██████████| 2/2 [00:00<00:00, 4725.98it/s]
2026-04-22T18:39:30.8924979Z .
2026-04-22T18:39:30.8925403Z Downloading assets:   0%|          | 0/2 [00:00<?, ?it/s]
2026-04-22T18:39:30.8926198Z Downloading assets: 100%|██████████| 2/2 [00:00<00:00, 4549.14it/s]
2026-04-22T18:39:30.8926724Z .
2026-04-22T18:39:30.8927160Z Downloading assets:   0%|          | 0/2 [00:00<?, ?it/s]
2026-04-22T18:39:30.8927970Z Downloading assets: 100%|██████████| 2/2 [00:00<00:00, 4514.86it/s]
2026-04-22T18:39:30.8928851Z .F.......WARNING: Could not fetch size for asset: http://example.com/asset1
2026-04-22T18:39:30.8930432Z ............................Building topology for vector map <test@__temporal_t_stac_libstac_test_staclib_runnervmeorf1_67902>...
2026-04-22T18:39:30.8931319Z Registering primitives...
2026-04-22T18:39:30.8931567Z 
2026-04-22T18:39:30.8931732Z Building areas...
2026-04-22T18:39:30.8932083Z 0..50..100
2026-04-22T18:39:30.8932371Z Attaching centroids...
2026-04-22T18:39:30.8932702Z 0..50..100
2026-04-22T18:39:30.8933171Z WARNING: Number of centroids exceeds number of areas: 2 > 0
2026-04-22T18:39:30.8933961Z WARNING: Number of incorrect boundaries: 2
2026-04-22T18:39:30.8934574Z WARNING: Number of centroids outside area: 2
2026-04-22T18:39:30.8935647Z Building topology for vector map <test@__temporal_t_stac_libstac_test_staclib_runnervmeorf1_67902>...
2026-04-22T18:39:30.8936565Z WARNING: Number of centroids exceeds number of areas: 2 > 0
2026-04-22T18:39:30.8936923Z WARNING: Number of incorrect boundaries: 2
2026-04-22T18:39:30.8937226Z WARNING: Number of centroids outside area: 2
2026-04-22T18:39:30.8937496Z .....FAILED (failures=2)
2026-04-22T18:39:30.8937626Z 
2026-04-22T18:39:30.8937843Z ======================================================================
2026-04-22T18:39:30.8938621Z FAIL: test_all_assets_submitted (__main__.TestDownloadAssets.test_all_assets_submitted)
2026-04-22T18:39:30.8939129Z ----------------------------------------------------------------------
2026-04-22T18:39:30.8939464Z Traceback (most recent call last):
2026-04-22T18:39:30.8940014Z   File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/unittest/mock.py", line 1378, in patched
2026-04-22T18:39:30.8940699Z     return func(*newargs, **newkeywargs)
2026-04-22T18:39:30.8941104Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-04-22T18:39:30.8941616Z   File "temporal/t.stac/libstac/testsuite/test_staclib.py", line 676, in test_all_assets_submitted
2026-04-22T18:39:30.8942138Z     self.assertEqual(mock_executor.submit.call_count, n)
2026-04-22T18:39:30.8942492Z AssertionError: 4 != 3
2026-04-22T18:39:30.8942623Z 
2026-04-22T18:39:30.8942784Z ======================================================================
2026-04-22T18:39:30.8943306Z FAIL: test_pbar_updated_once_per_asset (__main__.TestDownloadAssets.test_pbar_updated_once_per_asset)
2026-04-22T18:39:30.8943834Z ----------------------------------------------------------------------
2026-04-22T18:39:30.8944152Z Traceback (most recent call last):
2026-04-22T18:39:30.8944659Z   File "/opt/hostedtoolcache/Python/3.11.15/x64/lib/python3.11/unittest/mock.py", line 1378, in patched
2026-04-22T18:39:30.8945131Z     return func(*newargs, **newkeywargs)
2026-04-22T18:39:30.8945403Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-04-22T18:39:30.8945932Z   File "temporal/t.stac/libstac/testsuite/test_staclib.py", line 747, in test_pbar_updated_once_per_asset
2026-04-22T18:39:30.8946452Z     self.assertEqual(mock_pbar.update.call_count, n)
2026-04-22T18:39:30.8946748Z AssertionError: 3 != 2
2026-04-22T18:39:30.8946866Z 
2026-04-22T18:39:30.8947064Z ----------------------------------------------------------------------
2026-04-22T18:39:30.8947362Z Ran 48 tests in 1.107s
2026-04-22T18:39:30.8947484Z 
2026-04-22T18:39:30.8947579Z FAILED (failures=2)
2026-04-22T18:39:30.8947851Z ========================================================================
2026-04-22T18:39:30.8948218Z FAILED ./temporal/t.stac/libstac/testsuite/test_staclib.py

execute_import_grass_raster()

def _run(pbar=None):
with ProcessPoolExecutor(max_workers=nprocs) as executor:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude (see below) doesn't think ProcessPoolExceutor is a good choice, what was the reason for the switch?

Why ProcessPoolExecutor? The actual work — r.import — is already spawned as a subprocess by gs.parse_command, so the task is I/O-bound from Python's perspective and the GIL is released during the subprocess wait. ThreadPoolExecutor was the appropriate choice. Switching to processes
adds pickling + process-spawn overhead (especially on macOS/Windows where spawn re-imports staclib and the full pygrass stack) with no parallelism gain. Both real fixes — the cap of 8 and the GDAL env vars — work identically with threads. Unless there's a concrete reason (a GRASS
fork-safety issue?), I'd keep ThreadPoolExecutor and drop the manual sliding-window machinery entirely; executor.map already bounds in-flight work to max_workers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProcessPoolExecutor gives each worker its own env and its own GRASS session, which eliminates race conditions WIND_OVERRIDE, GIS_LOCK, and region state.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each r.import has its own process, so I don't see how this applies. Concurrent r.import should work, if not, that's a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants